﻿using Microsoft.Extensions.Logging;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WMS.Service.Redis
{
    public class RedisService : IRedisService
    {
        private readonly IDatabase _database;
        private readonly ILogger<RedisService> _logger;

        public RedisService(IDatabase database, ILogger<RedisService> logger)
        {
            _database = database;
            _logger = logger;
        }

        /// <summary>
        /// 设置缓存
        /// </summary>
        public async Task<bool> SetAsync<T>(string key, T value, TimeSpan? expiry = null)
        {
            try
            {
                var json = System.Text.Json.JsonSerializer.Serialize(value);
                return await _database.StringSetAsync(key, json, expiry);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Redis设置缓存失败 - Key: {key}");
                return false;
            }
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        public async Task<T?> GetAsync<T>(string key)
        {
            try
            {
                var value = await _database.StringGetAsync(key);
                if (value.IsNullOrEmpty)
                    return default;

                return System.Text.Json.JsonSerializer.Deserialize<T>(value!);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Redis获取缓存失败 - Key: {key}");
                return default;
            }
        }

        /// <summary>
        /// 设置字符串缓存
        /// </summary>
        public async Task<bool> SetStringAsync(string key, string value, TimeSpan? expiry = null)
        {
            try
            {
                return await _database.StringSetAsync(key, value, expiry);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Redis设置字符串缓存失败 - Key: {key}");
                return false;
            }
        }

        /// <summary>
        /// 获取字符串缓存
        /// </summary>
        public async Task<string?> GetStringAsync(string key)
        {
            try
            {
                var value = await _database.StringGetAsync(key);
                return value.IsNullOrEmpty ? null : value.ToString();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Redis获取字符串缓存失败 - Key: {key}");
                return null;
            }
        }

        /// <summary>
        /// 删除缓存
        /// </summary>
        public async Task<bool> RemoveAsync(string key)
        {
            try
            {
                return await _database.KeyDeleteAsync(key);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Redis删除缓存失败 - Key: {key}");
                return false;
            }
        }

        /// <summary>
        /// 检查缓存是否存在
        /// </summary>
        public async Task<bool> ExistsAsync(string key)
        {
            try
            {
                return await _database.KeyExistsAsync(key);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Redis检查缓存存在失败 - Key: {key}");
                return false;
            }
        }
    }
}